using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Events;
using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components;
using Framework.Library.Components.Commands;

namespace Framework.Module.CMS.Admin.WorkFlow
{
    public partial class EventDetail : AuthenticatedControl
    {
        private EventInfo eventInfo;
        private int total;
        protected void Page_Load(object sender, EventArgs e)
        {
            btnRemoveCheck.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            if (Request.QueryString["eventid"] != null)
            {
                eventInfo = EventController.GetInfo(ConvertUtility.ToInt32(Request.QueryString["eventid"]));
                if (eventInfo == null) { this.Visible = false; return; }
                else
                {
                    lnkEvent.Text = eventInfo.Event_Name; lnkEvent.ToolTip = eventInfo.Event_Description;
                    lnkEvent.NavigateUrl = AppEnv.AdminUrlParams("cmseventlist");
                }
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string key = "Framework.Portal.CMS.Event";
            DataCaching data = new DataCaching();
            data.RemoveCache(key);

            DataTable source = EventController.Event_GetNewsByEventID(eventInfo.Event_ID, 0);
            total = source.Rows.Count;
            dgEventDetail.DataSource = source;
            dgEventDetail.DataBind();
        }       

        protected void dgEventDetail_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView curData = (DataRowView)e.Item.DataItem;
                HyperLink hplheadline = (HyperLink)e.Item.FindControl("hplheadline");
                hplheadline.Text = curData["Content_Headline"].ToString();
                hplheadline.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.CMS_ViewContent)) + "&contentid=" + curData["Distribution_ContentID"].ToString();
                e.Item.Cells[e.Item.Cells.Count - 1].Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
                DropDownList dropPriority = (DropDownList)e.Item.FindControl("dropPriority");
                MiscUtility.FillIndex(dropPriority, 0, total, (int)curData["Priority"]);
            }
        }

        protected void btnAddDistribution_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.CMS_DistributedList)) + "&eventid=" + eventInfo.Event_ID);
        }

        protected void btnUpdatePriority_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridItem item in dgEventDetail.Items)
                {
                    DropDownList dropPriority = (DropDownList)item.FindControl("dropPriority");
                    EventController.Event_SetDistributionPriority(eventInfo.Event_ID, Convert.ToInt32(item.Cells[0].Text), Convert.ToInt32(dropPriority.SelectedValue));
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
            }
        }

        protected void btnRemoveCheck_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (DataGridItem item in dgEventDetail.Items)
                {
                    CheckBox chkCheck = (CheckBox)item.FindControl("chkCheck");
                    if (chkCheck.Checked) EventController.Event_RemoveDistribution(eventInfo.Event_ID, Convert.ToInt32(item.Cells[0].Text));
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
            }
        }

        protected void dgEventDetail_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                try
                {
                    EventController.Event_RemoveDistribution(eventInfo.Event_ID, Convert.ToInt32(e.Item.Cells[0].Text));
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                }
                catch
                {
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
                }
            }
        }
    }
}